local propertiesDialog = nil

local matLight 		= Material( "sprites/light_icon" )
local matTrack		= Material( "sprites/dacre_track_b" )
local matBeam		= Material( "effects/lamp_beam" )
local matLightGrip	= Material( "effects/light_grip" )

ENT.RenderGroup 	= RENDERGROUP_TRANSLUCENT

local ShouldDrawIcon = true
include("shared.lua")

function ENT:CreateLight(identifier)

CreateConVar(identifier .. "Radius", "100")
CreateConVar(identifier .. "Angle", "0")
CreateConVar(identifier .. "Bright", "10")
CreateConVar(identifier .. "Size", "100")

end

function ENT:Initialize()

	self:CreateLight(BACKLIGHT_IDENT)
	self:CreateLight(KEYLIGHT_IDENT)
	self:CreateLight(FILLLIGHT_IDENT)
	self.PixVis = util.GetPixelVisibleHandle()
	
end

/*---------------------------------------------------------
   Name: Draw
---------------------------------------------------------*/
function ENT:Draw()

end

function ENT:DrawTrack(lightData)

	if(lightData) then

		render.SetMaterial(matTrack)

		local curvRes = 13
		if(lightData.Radius < 250) then
			if(lightData.Radius > 100) then
				curvRes = 10
			elseif(lightData.Radius > 150) then
				curvRes = 7
			elseif(lightData.Radius > 50) then
				curvRes = 4
			elseif(lightData.Radius > 25) then
				curRes = 2
			else
				curvRes = 1
			end
		end

		//Adding '2' ensures we dont get an ugly seam/join
		render.StartBeam((4 * curvRes) + 2)
		for i = 1, ((4 * curvRes) + 2) do

			local angle = (i * 360) / (4 * curvRes)

			local pointOffest = Vector(math.sin(math.rad(angle)), math.cos(math.rad(angle)), 0)
			local pointPosition = self:GetPos() + (pointOffest * lightData.Radius)
			render.AddBeam(pointPosition, 3, i, Color(255,255,255,255))

		end
		render.EndBeam()

	end
end

function ENT:DrawLight(lightData)

	if (lightData) then
		local offset = Vector(0,0,0)
		offset.x = math.cos(math.rad(lightData.Angle))
		offset.y = math.sin(math.rad(lightData.Angle))
		offset.z = 0

		local position = self:GetPos() + (offset * lightData.Radius)
		local pixelVis = util.PixelVisible( position, 4, self.PixVis )
		if (pixelVis && pixelVis != 0) then 
			render.DrawSprite( position, 16, 16, lightData.Color)
		end

	end
end

function ENT:UpdateLight(lightData)

	if (lightData) then
		local lightObject = DynamicLight(  self:EntIndex() + lightData.Index )
		if (lightObject) then
		
			local offset = Vector(0,0,0)
			offset.x = math.cos(math.rad(lightData.Angle))
			offset.y = math.sin(math.rad(lightData.Angle))
			offset.z = 0
			
			lightObject.Pos = self:GetPos() + (offset * lightData.Radius)
			lightObject.r = lightData.Color.r
			lightObject.g = lightData.Color.g
			lightObject.b = lightData.Color.b
			lightObject.Brightness = lightData.Brightness
			lightObject.Size = lightData.Size
			lightObject.Decay = 1
			lightObject.DieTime = CurTime() + 1
		end

	end
end



/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()

	local backLight = self:RetrieveLightData(BACKLIGHT_IDENT)
	local keyLight = self:RetrieveLightData(KEYLIGHT_IDENT)
	local fillLight = self:RetrieveLightData(FILLLIGHT_IDENT)

	self:UpdateLight(backLight)
	self:UpdateLight(keyLight)
	self:UpdateLight(fillLight)

	local lightDataBL = {}
	lightDataBL.Angle = GetConVarNumber(BACKLIGHT_IDENT .. "Angle")
	lightDataBL.Radius = GetConVarNumber(BACKLIGHT_IDENT .. "Radius")
	lightDataBL.Color = backLight.Color
	lightDataBL.Index = backLight.Index
	lightDataBL.Size = GetConVarNumber(BACKLIGHT_IDENT .. "Size")
	lightDataBL.Brightness = GetConVarNumber(BACKLIGHT_IDENT .. "Bright")
	self:StoreLightData( BACKLIGHT_IDENT, lightDataBL)

		 
	local lightDataFL = {}
	lightDataFL.Angle = GetConVarNumber(FILLLIGHT_IDENT .. "Angle")
	lightDataFL.Radius = GetConVarNumber(FILLLIGHT_IDENT .. "Radius")
	lightDataFL.Color = fillLight.Color
	lightDataFL.Index = fillLight.Index
	lightDataFL.Size = GetConVarNumber(FILLLIGHT_IDENT .. "Size")
	lightDataFL.Brightness = GetConVarNumber(FILLLIGHT_IDENT .. "Bright")
	self:StoreLightData( FILLLIGHT_IDENT, lightDataFL)


	local lightDataKL = {}
	lightDataKL.Angle = GetConVarNumber(KEYLIGHT_IDENT .. "Angle")
	lightDataKL.Radius = GetConVarNumber(KEYLIGHT_IDENT .. "Radius")
	lightDataKL.Color = keyLight.Color
	lightDataKL.Index = keyLight.Index
	lightDataKL.Size = GetConVarNumber(KEYLIGHT_IDENT .. "Size")
	lightDataKL.Brightness = GetConVarNumber(KEYLIGHT_IDENT .. "Bright")
	self:StoreLightData( KEYLIGHT_IDENT, lightDataKL)

	self.Entity:NextThink(CurTime() + 2)
	print("-----")
end

function BuildLightPanel(identifier, parentPanel)
	
	local lightPanel = vgui.Create( "DPanel", parentPanel) 

	local sizeX,sizeY = parentPanel:GetSize()
	sizeX = sizeX - 5
	sizeY = sizeY - 5

	lightPanel:SetPos( 5, 10 ) 
	lightPanel:SetSize( sizeX, sizeY) 
	lightPanel.Paint = function()
	 surface.SetDrawColor( 50, 50, 50, 255 )
	 surface.DrawRect( 0, 0, lightPanel:GetWide(), lightPanel:GetTall() )
	end 

	local anchorX, anchorY = lightPanel:GetPos()

	local trackLabel = vgui.Create("DLabel", lightPanel) 
	trackLabel:SetText("Track:") 
	trackLabel:SetTextColor(Color(255,255,255,255)) 
	trackLabel:SizeToContents()
	trackLabel:SetPos(anchorX + 5,anchorY)

	local angleSlider = vgui.Create( "DNumSlider", lightPanel ) 
	angleSlider:SetPos(anchorX + 5, anchorY + 15)
	angleSlider:SetSize(155, 100)
	angleSlider:SetText("Angle")
	angleSlider:SetMin( 0 )
	angleSlider:SetMax(360)
	angleSlider:SetDecimals(1)
	angleSlider:SetConVar( identifier .. "Angle" )

	local radiusSlider = vgui.Create( "DNumSlider", lightPanel ) 
	radiusSlider:SetPos(anchorX + 5, anchorY + 55)
	radiusSlider:SetSize(185, 100)
	radiusSlider:SetText("Radius")
	radiusSlider:SetMin( 0 )
	radiusSlider:SetMax(1000)
	radiusSlider:SetDecimals(1)
	radiusSlider:SetConVar( identifier .. "Radius" )

	local propertiesLabel = vgui.Create("DLabel", lightPanel) 
	propertiesLabel:SetText("Light:") 
	propertiesLabel:SetTextColor(Color(255,255,255,255)) 
	propertiesLabel:SizeToContents()
	propertiesLabel:SetPos(anchorX + 5,anchorY + 100)

	local sizeSlider = vgui.Create( "DNumSlider", lightPanel ) 
	sizeSlider:SetPos(anchorX + 5, anchorY + 115)
	sizeSlider:SetSize(185, 100)
	sizeSlider:SetText("Size")
	sizeSlider:SetMin( 0 )
	sizeSlider:SetMax(400)
	sizeSlider:SetDecimals(0)
	sizeSlider:SetConVar( identifier .. "Size" )

	local brightSlider = vgui.Create( "DNumSlider", lightPanel ) 
	brightSlider:SetPos(anchorX + 5, anchorY + 155)
	brightSlider:SetSize(185, 100)
	brightSlider:SetText("Brightness")
	brightSlider:SetMin( 0 )
	brightSlider:SetMax(10)
	brightSlider:SetDecimals(1)
	brightSlider:SetConVar( identifier .. "Bright" )

	return lightPanel

end

function ShowProperties(userMessage)

	local callingEntity = userMessage:ReadEntity()
	local screenPos = callingEntity:GetPos():ToScreen()

	ShouldDrawIcon = false

	if(propertiesDialog) then
		propertiesDialog:SetVisible(true)
	else
		propertiesDialog = vgui.Create( "DFrame" ) 
		propertiesDialog:SetPos( screenPos.x + 30,screenPos.y + 30) 
		propertiesDialog:SetSize( 400, 300 ) 
		propertiesDialog:SetTitle( "Properties")
		propertiesDialog:SetVisible( true ) 
		propertiesDialog:SetDraggable( false )
		propertiesDialog:ShowCloseButton( false )
		propertiesDialog:MakePopup() 
	    
		local propertiesSheet = vgui.Create( "DPropertySheet" )
		propertiesSheet:SetParent( propertiesDialog )
		propertiesSheet:SetPos( 5, 30 )
		propertiesSheet:SetSize( 400 - (5 * 2), 300 - (30 + 5 + 20 + 5))

		local backLightPanel = BuildLightPanel(BACKLIGHT_IDENT,propertiesSheet)
		local keyLightPanel = BuildLightPanel(KEYLIGHT_IDENT,propertiesSheet)
		local fillLightPanel = BuildLightPanel(FILLLIGHT_IDENT,propertiesSheet)

		propertiesSheet:AddSheet( "Back Light", backLightPanel, "gui/silkicons/user", false, false, "Back Light controls" )
		propertiesSheet:AddSheet( "Key Light", keyLightPanel, "gui/silkicons/user", false, false, "Key Light controls" )
		propertiesSheet:AddSheet( "Fill Light", fillLightPanel, "gui/silkicons/user", false, false, "Fill Light controls" )

		local okButton = vgui.Create( "DButton" ) 
		okButton:SetParent( propertiesDialog ) // Set parent to our "DermaPanel" 
		okButton:SetText( "OK" ) 
		okButton:SetPos( 5, 275 ) 
		okButton:SetSize( 60, 20 ) 
		okButton.DoClick = function () 
			propertiesDialog:SetVisible(false)
			ShouldDrawIcon = true
		end  
   end
   
end

usermessage.Hook( "req_ShowProp", ShowProperties ) 

/*---------------------------------------------------------
   Name: DrawTranslucent
   Desc: Draw translucent
---------------------------------------------------------*/
function ENT:DrawTranslucent()

	if LocalPlayer():GetNWInt("show_objects") == 0 then
		return
	end

	if ShouldDrawIcon then
		render.SetMaterial( matLightGrip )
		render.DrawSprite( self:GetPos() , 32, 32, Color(255,255,255,255))
	end

	render.SetMaterial( matLight )
	
	local backLight = self:RetrieveLightData(BACKLIGHT_IDENT)
	local keyLight = self:RetrieveLightData(KEYLIGHT_IDENT)
	local fillLight = self:RetrieveLightData(FILLLIGHT_IDENT)

	self:DrawLight(backLight)
	self:DrawLight(keyLight)
	self:DrawLight(fillLight)

	self:DrawTrack(backLight)
	self:DrawTrack(keyLight)
	self:DrawTrack(fillLight)
end

